package com.bj58.ecdata.dashboard.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.constant.EnumPlatform;
import com.bj58.ecdata.dashboard.constant.NetFlowIndex;
import com.bj58.ecdata.dashboard.databll.NetFlowBLL;
import com.bj58.ecdata.dashboard.dbentity.NetFlowSource;
import com.bj58.ecdata.dashboard.dbentity.SourceFlow;
import com.bj58.ecdata.dashboard.dbentity.TrafficFlow;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;
import net.sf.json.JSONObject;

@Path("/netflow")
public class NetFlowAjaxController extends MvcController {
	private AbstractBaseService<NetFlowSource> nfSourceService = ServiceBeanFactory.get(NetFlowSource.class,null);
	private static AbstractBaseService<TrafficFlow> trafficFlowService = ServiceBeanFactory.get(TrafficFlow.class,null);
	private static AbstractBaseService<SourceFlow> sourceFlowService = ServiceBeanFactory.get(SourceFlow.class,null);
	
	@POST
	@Path("/ajax/{cate:\\S+}/{index:\\S+}")
	public ActionResult getDataByCateIndex(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			//index参数不合法时会有illegalArgumentException异常
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String data = NetFlowBLL.getLine_Day(cateEnum, indexEnum,dateGroup);
//			log.info(cate+"--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.view("null");
		}
	}
	
	
	@Path("/ajax/platform/{cate:\\S+}/{index:\\S+}")
	@POST
	public ActionResult getPlatformData(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
//			Model model = beat.getModel();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);

			String data = NetFlowBLL.getPlatformJson(cateEnum, indexEnum,dateGroup);
//			log.info(cate+"--platform--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取 “三端流量分别占总三端的比值” 数据的data
	 * @param cate
	 * @param index
	 * @return
	 */
	@Path("/ajax/platformRate")
	@POST
	public ActionResult getPlatformRateData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");

			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getPlatformRateJson(cateEnum, indexEnum,dateGroup);
//			log.info(businessName+"-"+indexEnum+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
	
	@Path("/ajax/cateTotal/{cate:\\S+}/{index:\\S+}")
	@POST
	public ActionResult getCateTotalData(String cate, String index) {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			indexEnum = NetFlowIndex.valueOf(index);
			cateEnum = EnumCate.valueOf(cate);
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String data = NetFlowBLL.getCateTotal_DayJson(cateEnum,indexEnum,dateGroup);
			
//			log.info("cateTotal--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}

	}
	
	/**
	 * 全站top UV 热力图
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-09
	 */
	@Path({ "/ajax/heatCityUV" })
	@POST
	public ActionResult topNCity() {
		try{
			HttpServletRequest request = beat.getRequest();
			//平台
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", EnumPlatform.all.getColName());
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			//日期
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date", "");
			if(StringUtils.isBlank(stat_date)){
				stat_date = DateUtil.getYestoday("yyyy-MM-dd");
			}
			
			String jsonArray = NetFlowBLL.getUVCityJson(stat_date,platEnum, cateEnum);
			return ViewResult.jsonView(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.jsonView("[]");
		}
	}

	/**
	 * 月独立MAU数据
	 * @param cate
	 * @param index
	 * @return
	 */
	@Path("/ajax/mau")
	@POST
	public ActionResult getMAUData(String cate, String index) {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "mau");

			String data = NetFlowBLL.getMAUDataJson(businessName, searchIndex);
//			log.info(cate+"--platform--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}

	
	@Path("/ajax/source")
	@POST
	public ActionResult getSourceData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			String sourceField = MyServletRequestUtils.getStringParameter(request, "sourceLevel", "source");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getSourceDataJson(cateEnum, indexEnum,platEnum,sourceField,dateGroup);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	@Path("/ajax/sourceTwo")
	@POST
	public ActionResult getSourceTwoData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			String source1_name = MyServletRequestUtils.getStringParameter(request, "source1_name", "SEO");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);

			String data = NetFlowBLL.getSourceDataTwoJson(cateEnum, indexEnum,platEnum,source1_name,dateGroup);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

//	@Path("/ajax/sourceThree")
//	@POST
//	public ActionResult getSourceThreeData() {
//		NetFlowIndex indexEnum = null;
//		EnumCate cateEnum = null;
//		try {
//			HttpServletRequest request = beat.getRequest();
//			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
//			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
//			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
//			String source2_name = MyServletRequestUtils.getStringParameter(request, "source2_name", "付费浏览器");
//			
//			indexEnum = NetFlowIndex.valueOf(searchIndex);
//			cateEnum = EnumCate.valueOf(businessName);
//
//			String data = NetFlowBLL.getSourceDataThreeJson(cateEnum, indexEnum,source2_name,dateGroup);
////			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"---"+data);
//			return ViewResult.jsonView(data);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return ViewResult.view("{}");
//		}
//	}
	
	@Path("/ajax/sourceTwoPie")
	@POST
	public ActionResult getSourceTwoPieData() {
		NetFlowIndex indexEnum = null;
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","total");
			String plat = MyServletRequestUtils.getStringParameter(request, "platform", "pc");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			cateEnum = EnumCate.valueOf(businessName);
			if(StringUtils.isBlank(beginDate)){
				beginDate = nfSourceService.getMinDate();
			}
			if(StringUtils.isBlank(endDate)){
				endDate = nfSourceService.getMaxDate();
			}
			
			String data = NetFlowBLL.getSourceTwoPieDataJson(cateEnum, indexEnum,platEnum,beginDate,endDate);
//			log.info(businessName+"--platform--"+plat+"--"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
		
	}
	
	@Path("/ajax/cityCateTwo")
	@POST
	public ActionResult getCityCateData() {
		NetFlowIndex indexEnum = null;
		EnumCate busEnum = null;
		try {
			HttpServletRequest request = beat.getRequest();
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String city1 = MyServletRequestUtils.getStringParameter(request, "city1","all");
			String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "indexType", "pv");
			
			indexEnum = NetFlowIndex.valueOf(searchIndex);
			busEnum = EnumCate.valueOf(businessName);
			String data = NetFlowBLL.getCityCate2Json(busEnum,city1,cate2,indexEnum,dateGroup);
//			log.info(businessName+":"+city1+":"+searchIndex+"\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	/**
	 * 获取流量来源  “饼图3级来源”
	 * @return
	 */
	@Path("/ajax/source3Pie")
	@POST
	public ActionResult source3Pie() {
		try {
			HttpServletRequest request = beat.getRequest();
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate", "2015-01-01");
			String endDate = MyServletRequestUtils.getStringParameter(request, "endDate", "2015-01-01");
			
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform", "m");
			String source2 = MyServletRequestUtils.getStringParameter(request, "source2", "");
			EnumPlatform plat = EnumPlatform.valueOf(platform);
			
			String data = NetFlowBLL.getSource3PieDataJson(businessName, source2, plat, beginDate, endDate);
//			log.info(businessName+"--"+source1+"---\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取流量来源  “曲线图3级来源”
	 * @return
	 */
	@Path("/ajax/source3Line")
	@POST
	public ActionResult source3Line() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","zhaopin");
			String dateGroup = MyServletRequestUtils.getStringParameter(request, "dateGroup", "stat_date");
			String searchIndex = MyServletRequestUtils.getStringParameter(request, "index", "uv");
			String source2 = MyServletRequestUtils.getStringParameter(request, "source2", "站内");
			String source3s = ServletRequestUtils.getStringParameter(request, "source3s", "");
			NetFlowIndex index = NetFlowIndex.valueOf(searchIndex);
			
			String data = NetFlowBLL.getSource3LineDataJson(businessName, dateGroup, source2, source3s, index);
//			log.info(businessName+"--\n"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
		
	/**
	 * 获取访问路径
	 * @return
	 */
	@Path("/ajax/visit_path")
	@POST
	public ActionResult visitPath() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			if("total".equals(businessName)){
				businessName="all";
			}
			String data = NetFlowBLL.getVisitPathDataJson(businessName, platform, stat_date);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 *   流量传递及消息
	 */
	
	@Path("/ajax/{option}")
	@POST
	public ActionResult trafficFlow(String option) {
		try {
			HttpServletRequest request = beat.getRequest();
			String business_from = MyServletRequestUtils.getStringParameter(request, "business_from","");
			String business_to = MyServletRequestUtils.getStringParameter(request, "business_to","");
			String page_from = MyServletRequestUtils.getStringParameter(request, "page_from","");
			String page_to = MyServletRequestUtils.getStringParameter(request, "page_to","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");		
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");	
			String plat = MyServletRequestUtils.getStringParameter(request, "plat","");
			String data = "";
		
		    if("trafficFlow".equals(option)){		    	
		    	String cate1_from = MyServletRequestUtils.getStringParameter(request, "cate1_from","all");
		    	String cate1_to = MyServletRequestUtils.getStringParameter(request, "cate1_to","all");
		    	data = NetFlowBLL.getFlowDataJson(plat,business_from,business_to, cate1_from,cate1_to,page_from,page_to,platform,stat_date,option);
		    }else{
		    	String source_from = MyServletRequestUtils.getStringParameter(request, "source_from","all");
		    	String source_to = MyServletRequestUtils.getStringParameter(request, "source_to","all");
		    	data = NetFlowBLL.getFlowDataJson(plat,business_from,business_to, source_from,source_to,page_from,page_to,platform,stat_date,option);
		    }
			
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 *   获取类别
	 */
	
	@Path("/ajax/getcate/trafficflow")
	@POST
	public ActionResult getCateTraffiflow(String option) {
		try {
			HttpServletRequest request = beat.getRequest();
			String business_from_or_to = MyServletRequestUtils.getStringParameter(request, "business_from_or_to","");
			String business = MyServletRequestUtils.getStringParameter(request, "business","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			String plat = MyServletRequestUtils.getStringParameter(request, "plat","");
			String data = "";
			if("total".equals(business)){
				business = "all";
			}
		    data = NetFlowBLL.getCateByBusiness(plat,business_from_or_to,business,stat_date, "trafficflow");
		    	
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 *   获取类别
	 */
	
	@Path("/ajax/getcate/source")
	@POST
	public ActionResult getCateSource(String option) {
		try {
			HttpServletRequest request = beat.getRequest();
			String business_from_or_to = MyServletRequestUtils.getStringParameter(request, "business_from_or_to","");
			String business = MyServletRequestUtils.getStringParameter(request, "business","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			String plat = MyServletRequestUtils.getStringParameter(request, "plat","");
			String data = "";
			if("total".equals(business)){
				business = "all";
			}
		    data = NetFlowBLL.getCateByBusiness(plat,business_from_or_to,business,stat_date, "source");
		    
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 切换平台是获取最大的日期
	 */
	@Path("/ajax/setMaxDate")
	@POST
	public ActionResult getMaxDate(){
		
		HttpServletRequest request = beat.getRequest();
		String plat = MyServletRequestUtils.getStringParameter(request, "plat","");
		String option = MyServletRequestUtils.getStringParameter(request, "option","");
		JSONObject json = new JSONObject();
		try{
			String date = DateUtil.getYestoday("yyyyMM");
			String maxDate = "";
			if("source".equals(option)){
				 maxDate = sourceFlowService.getMaxDateByTableSuffix(plat+"_source_"+date);
			}else{			
				 maxDate = trafficFlowService.getMaxDateByTableSuffix(plat+"_cate_"+date);
			}
				
			
			json.put("maxDate", maxDate);
			json.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			json.toString();
			
		}catch(Exception e){
			e.printStackTrace();
			return ViewResult.view("{}");
		}
		return ViewResult.jsonView(json.toString());
	}
	
	/**
	 * 获取访问路径-品类        获取访问路径-来源
	 * @return
	 */
	@Path("/ajax/visit_pathNew")
	@POST
	public ActionResult visitPathNew() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date","");
			String cate =  MyServletRequestUtils.getStringParameter(request, "cate_name","");
			String actionType =  MyServletRequestUtils.getStringParameter(request, "actionType","");
			String plat =  MyServletRequestUtils.getStringParameter(request, "plat","");
			String option = MyServletRequestUtils.getStringParameter(request, "option","");
			if("total".equals(businessName)){
				businessName="all";
			}
			String data = NetFlowBLL.getVisitPathNewDataJson(businessName, platform, stat_date,cate,actionType,plat,option);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 获取访问路径的类别
	 * @return
	 */
	@Path("/ajax/visit_pathcate")
	@POST
	public ActionResult visitPathCate() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName","");
			String platform = MyServletRequestUtils.getStringParameter(request, "platform","");
			if("total".equals(businessName)){
				businessName="all";
			}
			List<String> list = null;
			if("".equals(platform)){		
				list = NetFlowBLL.getPathCate("cate_name","biz_name='"+businessName+"'");
			}else{
				list = NetFlowBLL.getPathCate("source","biz_name='"+businessName+"' AND platform='"+platform+"'");
			}
			JSONObject json = new JSONObject();
			json.put("cate", list);
//			log.info(data);
			return ViewResult.jsonView(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
}
